05. MISRA C++ Lab
MISRA Lab
The MISRA C++ Standard
The MISRA C++ Standard defines a subset of C++ appropriate for safety critical automative applications. The standard includes hundreds of rules about how to use the C++ language when developing automotive software.
Oftentimes, software engineers use a code compliance checking package to ensure that their code meets the standard. In this lab, you will receive working C++ Kalman filter code that contains numerous MISRA violations. Your job will be to fix the errors and get the code as close to compliancy as possible.
MathWorks is providing you a trial version of their MISRA compliance checking software, which is called Polyspace. Polyspace is one of the most common software programs used in industry for writing MISRA compliant C++ code. The program is part of a larger suite of mathematical and engineering tools called MATLAB.
Here is a video about MATLAB and how it is used in engineering and science.
What Is MATLAB SelfDriving
Polyspace Use Case
Also, before you start the lab, check out this use case for how one car company uses Polyspace to improve its code quality: link to use case.
Installing MATLAB Polyspace
As a result of support from MathWorks, a free downloadable license to MATLAB® and Polyspace® is
available for the duration of the course.
- If you do not already have a MathWorks account, create a new account.
- Access your free license to MATLAB here: download link.
If you have a problem installing MATLAB, search or post your question in Knowledge or in your Student Hub in the Functional Safety project channel. Please provide your OS and a list of steps taken prior the error. A screen shot of the error is often helpful as well.
You can find the system requirements for MATLAB on the MathWorks website.
Setting up the Lab
Once you have installed MATLAB Polyspace, you are ready to start the lab. At the bottom of this page, you will see a link that says "Supporting Materials: Psprj for Udacity". Download and unzip the folder.
In a text editor, open fileroot/Polyspace/kalmanFilterProject_original.psprj. You will see include paths on lines 13-18, lines 34-39, and line 45. They should be replaced with the respective paths to your local folders. Save and exit the text editor.
For example,
On a Mac:
<file path="file:/Users/UserName/Desktop/polyspace/psprj_for_udacity/include/usr/lib/gcc/x86_64-linux-gnu/4.9/include/" order="0"/>
On a Windows machine:
<file path="file:/C:/Users/UserName/Desktop/psprj_for_udacity/include/usr/lib/gcc/x86_64-linux-gnu/4.9/include/" order="0"/>
Launch Polyspace
Launch Polyspace with the shortcut
matlabroot/polyspace/bin/polyspace.exe
where matlabroot is the MATLAB installation directory (usually called MATLAB/r2017a)
On a mac, you can also launch the program by:
- going to the applications folder
- right click on "MATLAB_R2017a" and choose "Show Package Contents"
- click on the polyspace folder then bin folder (polyspace -> bin)
- right click on "polyspace" and choose "open"
Open the Project File and Run the Bug Finder
Open the project file, fileroot/Polyspace/kalmanFilterProject_original.psprj in the Polyspace window.
Click "Run Bug Finder". MISRA C++ Results should appear automatically.
You might see a benign popup error during the analysis. You can click to end the errored background process, and the analysis will complete normally. This error does not affect the MISRA analysis. This error generally does not appear on Macs, but it can appear on Linux and Windows systems.
Complete the Lab
Overview of the Software
When you first open polyspace, the program provides an overview of how the software works. Here are the main things to pay attention to:
- Project Browser tab: shows the structure of the Polyspace project
- Results List tab: a list of MISRA violations
- Run Bug Finder button: checks code for MISRA compliance
- Configuration window: no need to change anything here
- Dashboard: shows progress of the bug analysis
- Output summary: summary results of the analysis
To do the lab:
- The code for this lab should look somewhat familiar. It is an extended kalman filter.
- After opening the .psprj file, click "Run Bug Finder", which is located at the top of the window.
- The analysis will run, and you can watch the progress in the "Output Summary" tab. The analysis is relatively short (3-4 minutes seems about average)
- The "Results List" tab will show all of the MISRA violations
- You can click on the different violations, which will open the code in the "Source" tab.
- If you right click on a violation, you have the option to open the code in an editor: "Open Editor". You can then edit and save your code to fix the violation.
- In the "Source" tab, you can also scroll through the code to see where there are violations. Violations are marked with purple triangles, and clicking on the triangle will highlight the violation.
- In the "Results Details" window, if you click on the question mark, a "Contextual Help" window opens. The contextual help gives a list of all the MISRA C++ rules. Reading through the rules can be helpful for understanding what is wrong with the code.
Every time you want to see if your code is fixed, you need to click on "Run Bug Finder" again. If you want to only run the analysis on one file at a time, go to:
"Project Browswer" tab -> Project Source Files -> src original
And then right click on files that you want to exclude from the analysis and choose "exclude files". You'll notice that "main.cpp" was excluded from the analysis so that you could just focus on the kalman filter code.
Extra Challenge
Try fixing your own project code for MISRA violations.